<template>
  <div class="article-create">
    <h1>{{props.id !== undefined ? '编辑文章' : '创建文章' }}</h1>
    <el-card>
      <el-input
        class="title-input"
        :placeholder="$t('msg.article.titlePlaceholder')"
        v-model="title"
        maxlength="20"
        clearable
      ></el-input>
      <el-tabs
        v-model="activeName">
        <el-tab-pane :label="$t('msg.article.markdown')" name="markdown">
          <markdown :title="title" :detail="detail" @onSuccess="onSuccess"/>
        </el-tab-pane>
        <el-tab-pane :label="$t('msg.article.richText')" name="editor">
          <editor :title="title" :detail="detail" @onSuccess="onSuccess"/>
        </el-tab-pane>
      </el-tabs>
    </el-card>
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue'
import Editor from './componnets/Editor.vue'
import Markdown from './componnets/Markdown.vue'
import { articleDetail } from '../../../api/article'
const props = defineProps({
  id: {
    required: false,
    type: String
  },
  detail: {
    type: Object
  }
})
const title = ref('')
const detail = ref({})
const activeName = ref('markdown')
const onSuccess = () => {
  title.value = ''
}
const getArticleDetail = async () => {
  detail.value = await articleDetail(props.id)
  // 标题赋值
  title.value = detail.value.title
}
if (props.id !== undefined) {
  getArticleDetail()
}
</script>
<style lang="scss" scoped>
.title-input {
  margin-bottom: 20px;
}
</style>
